#!/usr/bin/env python
# coding: utf-8

# # 4.增加人脸入数据集合

# ## 1.人脸检测（功能）

# In[1]:


import detect
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq',
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'


# In[2]:


help(detect)


# In[3]:


detect.face_detect(API_key,API_secret,'yyqx.jpg')


# In[4]:


with open('yyqx.jpg','rb')as f:
    data = f.read()
print(data)


# ## 2.人脸分析

# In[5]:


import analyze
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq',
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'


# In[6]:


face = detect.face_detect(API_key,API_secret,'yyqx.jpg')


# In[7]:


face_tokens1 = face['faces'][0]['face_token']
face_tokens1


# In[8]:


help(analyze)


# In[9]:


analyze.face_analyze(API_key,API_secret,face_tokens1)


# ## 3.人脸对比

# In[10]:


import compare


# In[11]:


face2 = detect.face_detect(API_key,API_secret,'yyqx1.jpg')


# In[12]:


face_tokens2 = face2["faces"][0]["face_token"]
face_tokens2


# In[13]:


help(compare)


# In[14]:


compare.face_compare(API_key,API_secret,face_tokens1,face_tokens2)


# ## 4.人脸搜索

# In[37]:


import search


# In[38]:


base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'


# In[39]:


payload = {
    'api_key':API_key,
    'api_secret':API_secret,
    'face_tokens':'98bf2a79b9af4d8def1a4dcdacbbf6e9,4d0c3eac19bc8925a68c426046b85520'
}


# In[40]:


r = requests.post(base_url, params = payload )


# In[41]:


r.json()


# In[42]:


help(search)


# In[43]:


faceset_tokens = r.json()["faceset_token"]
faceset_tokens


# In[44]:


search.face_search(API_key,API_secret,faceset_tokens,face_tokens1)


# ## 5.宿舍门禁

# ### 创建FaceSet（创建人脸库 API）

# In[45]:


import requests
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq',
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'


# In[46]:


faceset_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'
payload = {
    'api_key':API_key,
    'api_secret':API_secret
}

r = requests.post(url = faceset_url,params = payload)


# In[47]:


r


# In[48]:


r.json()


# In[49]:


faceset_token = r.json()['faceset_token']
faceset_token


# ### 查询FaceSet（获取人脸库列表及信息 API）

# In[50]:


import requests
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq',
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'


# In[51]:


getfaceset_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'
payload = {
    'api_key':API_key,
    'api_secret':API_secret
}
r = requests.post(url = getfaceset_url,params = payload)


# In[52]:


r


# In[53]:


r.json()


# In[54]:


r.json()['facesets'][-1]


# In[55]:


faceset_token = r.json()['facesets'][-1]['faceset_token']
faceset_token


# ### 存入face_token数据

# #### 现获取人脸信息

# In[56]:


import detect
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq',
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'


# In[57]:


detect.face_detect(API_key,API_secret,'me.jpg')


# In[58]:


face_token_me = detect.face_detect(API_key,API_secret,'me.jpg')['faces'][0]['face_token']
face_token_me


# #### 存入face_token数据（添加人脸API）

# In[59]:


import requests
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq',
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'


# In[60]:


addfaceset_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'
payload = {
    'api_key':API_key,
    'api_secret':API_secret,
    'faceset_token':faceset_token,
    'face_tokens':face_token_me
}
r = requests.post(url=addfaceset_url,params=payload)


# In[61]:


r


# In[62]:


r.json()


# ### 获取FaceSet细节信息(获取人脸库信息 API)

# In[63]:


import requests
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq',
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'


# In[64]:


faceset_getdetail_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'
payload = {
    'api_key':API_key,
    'api_secret':API_secret,
    'faceset_token':faceset_token
}
r = requests.post(url = faceset_getdetail_url,params = payload)


# In[65]:


r


# In[66]:


r.json()


# ### 人脸搜索

# In[67]:


import detect
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq',
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'


# In[68]:


detect.face_detect(API_key,API_secret,'yyqx.jpg')


# In[69]:


face_token = detect.face_detect(API_key,API_secret,'yyqx.jpg')['faces'][0]['face_token']
face_token


# In[70]:


import search


# In[71]:


help(search)


# In[72]:


search.face_search(API_key,API_secret,face_token,faceset_token)


# In[73]:


search_url = 'https://api-cn.faceplusplus.com/facepp/v3/search'
payload = {
    'api_key':API_key,
    'api_secret':API_secret,
    'face_token':face_token,
    'faceset_token':faceset_token
}
r = requests.post(url = search_url,params = payload)


# In[74]:


r


# In[79]:


r.json()


# In[82]:


confidence = r.json()['results'][0]['confidence']
confidence


# In[84]:


if confidence >= 90:
     s = '是本人'
else:
    s =  '不是本人'


# In[85]:


s


# ### 人脸对比

# In[86]:


import compare
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq',
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'


# In[90]:


compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'
payload = {
    'api_key':API_key,
    'api_secret':API_secret,
    'face_token1':face_token,
    'face_token2':face_token_me
}
r = requests.post(url = compare_url,params = payload)


# In[91]:


r


# In[92]:


r.json()


# In[93]:


confidence = r.json()['confidence']
confidence


# In[94]:


if confidence >= 90:
     s = '是本人'
else:
    s =  '不是本人'


# In[95]:


s


# In[ ]:




